package com.goods.controller.business;


import com.goods.business.service.ManageService;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.system.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/business/productCategory")
@Api(tags = "业务管理-物资类别相关接口")
public class ProductCategoryController {

    @Autowired
    private ManageService manageService;

    /**
     * 物资分类列表
     *
     * @return
     */
    @ApiOperation(value = "物资分类列表")
    @GetMapping("/categoryTree")
    public ResponseBean<PageVO<ProductCategoryTreeNodeVO>> categoryTree( Integer pageNum,
                                                                         Integer pageSize) {
        PageVO<ProductCategoryTreeNodeVO> pageVO = manageService.categoryTree(pageNum,pageSize);
        return ResponseBean.success(pageVO);
    }


    /**
     * 物资分类父级列表
     *
     * @return
     */
    @ApiOperation(value = "物资分类父级列表")
    @GetMapping("/getParentCategoryTree")
    public ResponseBean<List<ProductCategoryTreeNodeVO>> getParentCategoryTree() {
        List<ProductCategoryTreeNodeVO> list= manageService.getParentCategoryTree();
        return ResponseBean.success(list);
    }

    /**
     * 添加分类
     *
     * @return
     */
    @ApiOperation(value = "添加分类")
    @PostMapping("/add")
    public ResponseBean add(@RequestBody ProductCategory productCategory) {
        manageService.add(productCategory);
        return ResponseBean.success();
    }

    /**
     * 修改回显分类
     *
     * @return
     */
    @ApiOperation(value = "修改回显分类")
    @GetMapping("/edit/{id}")
    public ResponseBean edit(@PathVariable("id") Long id) {
        ProductCategoryTreeNodeVO productCategoryTreeNodeVO = manageService.edit(id);
        return ResponseBean.success(productCategoryTreeNodeVO);
    }

    /**
     * 修改分类
     *
     * @return
     */
    @ApiOperation(value = "修改分类")
    @PutMapping("/update/{id}")
    public ResponseBean update(@PathVariable("id") Long id,@RequestBody ProductCategoryTreeNodeVO productCategory) {
        manageService.update(id,productCategory);
        return ResponseBean.success();
    }


    /**
     * 删除分类
     *
     * @return
     */
    @ApiOperation(value = "删除分类")
    @DeleteMapping("/delete/{id}")
    public ResponseBean delete(@PathVariable("id") Long id) {
        boolean b = manageService.delete(id);
        if (b) {
            return ResponseBean.success();
        }
        Map<String,String> map = new HashMap<>();
        map.put("errorMsg","分类删除失败");
        return ResponseBean.error(map);
    }


}
